package com.thinkgem.jeesite.modules.lottery.mobile;

import com.thinkgem.jeesite.common.utils.Result;
import com.thinkgem.jeesite.modules.lottery.entity.*;
import com.thinkgem.jeesite.modules.lottery.service.ActivityFormService;
import com.thinkgem.jeesite.modules.lottery.service.ActivityService;
import com.thinkgem.jeesite.modules.lottery.service.ActivityStatusService;
import com.thinkgem.jeesite.modules.lottery.service.ParticipationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/lottery/participation")
public class ParticipationMobileController {

    @Autowired
    private ActivityService activityService;
    @Autowired
    private ActivityStatusService activityStatusService;
    @Autowired
    private ActivityFormService activityFormService;
    @Autowired
    private ParticipationService participationService;

    @RequestMapping(value = "/getWechatUserActivity", method = RequestMethod.POST)
    @ResponseBody
    public Result getWechatUserActivity(String openid) {
        Result result = Result.ok("用户参与活动列表获取成功");
        List<Activity> activities = activityService.getWechatUserActivity(openid);

        result.put("wechatUserActivityList", getWechatUserActivityList(activities));
        return result;
    }

    @RequestMapping(value = "/getWechatUserOpenActivity", method = RequestMethod.POST)
    @ResponseBody
    public Result getWechatUserOpenActivity(String openid) {
        Result result = Result.ok("用户发起活动列表获取成功");
        List<Activity> activities = activityService.getWechatUserActivity(openid);

        result.put("wechatUserActivityList", getWechatUserActivityList(activities));
        return result;
    }

    @RequestMapping(value = "/getWechatUserPrizeActivity", method = RequestMethod.POST)
    @ResponseBody
    public Result getWechatUserPrizeActivity(String openid) {
        Result result = Result.ok("用户中奖记录获取成功");
        List<Activity> activities = activityService.getWechatUserPrizeActivity(openid);

        result.put("wechatUserActivityList", getWechatUserActivityList(activities));
        return result;
    }

    @RequestMapping(value = "/joinActivity",method = RequestMethod.POST)
    @ResponseBody
    public Result joinActivity(String activityId, String openid) {
        if (activityId.equals("") || activityId.equals("undefined") || openid.equals("") || openid.equals("undefined")) {
            return Result.error("用户参与活动失败");
        }

        Participation participation = new Participation();
        participation.setActivityId(activityId);
        participation.setOpenid(openid);
        participationService.save(participation);
        return Result.ok("用户参与活动成功");
    }

    private List<WechatUserActivity> getWechatUserActivityList(List<Activity> activities) {
        List<WechatUserActivity> wechatUserActivityList = new ArrayList<>();
        for (Activity activity : activities) {
            WechatUserActivity wechatUserActivity = new WechatUserActivity();
            wechatUserActivity.setActivityId(activity.getId());
            wechatUserActivity.setCreator(activity.getOpenid());
            wechatUserActivity.setName(activity.getName());
            wechatUserActivity.setImage(activity.getImage());
            wechatUserActivity.setEndTime(activity.getEndTime());

            ActivityStatus status = activityStatusService.get(activity.getStatusId());
            wechatUserActivity.setStatus(status.getDescription());
            ActivityForm form = activityFormService.get(activity.getFormId());
            wechatUserActivity.setForm(form.getDescription());

            wechatUserActivityList.add(wechatUserActivity);
        }
        return wechatUserActivityList;
    }

}
